package wortubung.lang.it;

import static wortubung.lang.LanguageManager.*;
import static wortubung.lang.it.ConiugatoreUtil.*;
import static wortubung.util.CommonUtils.chopRight;

import java.io.IOException;
import java.util.Properties;

import wortubung.core.LANG;
import wortubung.inflection.CONJUGATION;
import wortubung.inflection.UnknownConjugationException;
import wortubung.inflection.VerbConjugation;
import wortubung.lang.ConjugatorBase;
import wortubung.util.ImmutableStringList;
import wortubung.util.TransliterateException;

public class Coniugatore extends ConjugatorBase {

	public static final Coniugatore defaultConiugatore = new Coniugatore();

	static {
		defaultConiugatore.readInitFiles(IT_EXCEPCIONES_VERBALES_FILE, IT_CONIUGAZIONI_AS_IS_FILE);
	}

	@Override
	protected void furtherInit(Properties props) throws IOException, TransliterateException {
		readNameValuePairs(irregularPastParticiples, props.getProperty("irregular_past_participles"), true);
		readValues(iscVerbs, props.getProperty("isc_stems"), true);
	}

	public static VerbConjugation conjugate(String verb) { return defaultConiugatore.getConjugation(verb); }

	@Override public LANG getLanguage() { return LANG.it; }

	@Override
	protected String[] conjugate(String verbSuffix, CONJUGATION type, boolean unused) {
		if (verbSuffix.endsWith("arsi") || verbSuffix.endsWith("ersi") || verbSuffix.endsWith("irsI"))
			verbSuffix = chopRight(verbSuffix, 2) + 'e';

		int verbEnding = -1;
		if (iscVerbs.contains(verbSuffix))
			verbEnding = 3;
		else if (verbSuffix.endsWith("are"))
			verbEnding = 0;
		else if (verbSuffix.endsWith("ere"))
			verbEnding = 1;
		else if (verbSuffix.endsWith("ire"))
			verbEnding = 2;
		else
			UnknownConjugationException.badVerb(verbSuffix);

		String root = null;
		ImmutableStringList endings = null;
		switch(type) {
		case FUTURE:
			endings = FUTURO_INDICES;
			// fall thru
		case CONDITIONAL:
			if (endings == null)
				endings = CONDIZIONALE_INDICES;
			root = verbSuffix.endsWith("are") ? phonetique(chopRight(verbSuffix, 3), 0, "er") : chopRight(verbSuffix, 1);
			break;

		case SIMPLE_PAST:		endings = PASSATO_REMOTO              [verbEnding]; break;
		case IMPERFECT:			endings = IMPERFECTO_INDICES          [verbEnding]; break;
		case PRESENT:			endings = PRESENTE_INDICES            [verbEnding]; break;
		case IMPERATIVE:		endings = IMPERATIVO_INDICES          [verbEnding]; break;
		case SUBJ_PRESENT:		endings = CONGIUNTIVO_PRESENTE_INDICES[verbEnding]; break;

		case SUBJ_IMPERFECT:
			endings = CONGIUNTIVO_IMPERFETTO_INDICES;
			root = chopRight(verbSuffix, 2);
			break;
		}

		if (root == null)
			root = chopRight(verbSuffix, 3);

		return appendToRoot(root, verbEnding, endings);
	}

	protected String participle(String verbSuffix, boolean isPresent) {
		String ending;
		if (verbSuffix.endsWith("are"))
			ending = (isPresent ? "ante" : "ato");
		else if (verbSuffix.endsWith("ere"))
			ending = (isPresent ? "ente" : "uto");
		else // ire
			ending = (isPresent ? "ente" : "ito");
		return chopRight(verbSuffix, 3) + ending;
	}
}
